/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__hash_node_all_run.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2015/02/03 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frhash2.h"
#include "frlist.h"
#include "frsutl.h"
#include "frstat_ct.h"
#include "conntrack.h"
#include "module_comm.h"
#include "module.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

typedef struct __st_ct_conn_stat_l7 {
    int i_l7_proto;
    unsigned int ui_all_conn_count;
    unsigned int ui_dead_conn_count;

} Frct_conn_stat_l7;


typedef struct __st_stat_all {
    unsigned int ui_all_conn_count;
    unsigned int ui_dead_conn_count;

    //l7 协议分组统计信息
    Frhash_header *p_st_hash_l7;
} Fr_stat_all;

typedef struct __st_stat_l7 {
    Frct_conn_stat_l7 st_conn_stat_l7;

    int i_idx;

    int i_flag_use_sock;
    int accept_sock;

    char *p_buff_proto;
    int i_buff_proto_size;

    char *p_buff_ct;
    int i_buff_ct_size;
} Fr_stat_l7;

int __hash_node_all_run(Frlist_node* p_st_node, const void *p_data);

void __hash_node_conn_stat_free(Frlist_node *p_st_node);

void mylist_node_free(Frlist_node *p_st_node)
{
    L7_analyzer *p_l7analyzer = (L7_analyzer *)p_st_node->data;

    if(p_l7analyzer)
    {
        if(p_l7analyzer->p_st_l7_module)
            frfree(p_l7analyzer->p_st_l7_module);

        frfree(p_l7analyzer);
    }

    return;

}

void case1(void)
{
    Frlist_node *p_st_node = NULL;
    Fr_stat_all *p_st_stat_all = NULL;
    int i_ret = 0;

    i_ret = __hash_node_all_run(p_st_node,p_st_stat_all);

    CU_ASSERT_EQUAL(i_ret,1);
    return;

}

void case2(void)
{
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_st_node = NULL;
    Fr_stat_all st_stat_all = {0,0,NULL};
    int i_ret = 0;
    Frct_conn_stat_l7 *p_st_conn_stat_l7 = NULL;
    Frlist_node *p_st_node_l7_proto=NULL;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));

    gettimeofday (&p_st_ct->st_connection_lastskb_time, NULL);
    p_st_ct->st_connection_lastskb_time.tv_sec -= 100;

    p_st_node = frlist_node_create(p_st_ct);

    st_stat_all.p_st_hash_l7 = frhash2_create2(5);

    pthread_spin_init(&p_st_ct->lock,0);
    i_ret = __hash_node_all_run(p_st_node,&st_stat_all);
    pthread_spin_destroy(&p_st_ct->lock);

    CU_ASSERT_EQUAL(i_ret,1);
    CU_ASSERT_EQUAL(st_stat_all.ui_all_conn_count,1);
    CU_ASSERT_EQUAL(st_stat_all.ui_dead_conn_count,1);

    p_st_node_l7_proto = frhash2_getLong(st_stat_all.p_st_hash_l7, -2);

    p_st_conn_stat_l7 = (Frct_conn_stat_l7 *)(p_st_node_l7_proto->data);

    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_all_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_dead_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->i_l7_proto,-2);

    frhash2_free2(st_stat_all.p_st_hash_l7,__hash_node_conn_stat_free);


    frfree(p_st_ct);
    frfree(p_st_node);
    return;

}

void case3(void)
{
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_st_node = NULL;
    Fr_stat_all st_stat_all = {0,0,NULL};
    Frlist_node *p_st_node_l7_proto=NULL;
    int i_ret = 0;
    Frct_conn_stat_l7 *p_st_conn_stat_l7 = NULL;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));

    gettimeofday (&p_st_ct->st_connection_lastskb_time, NULL);
    p_st_ct->st_connection_lastskb_time.tv_sec -= 100;

    p_st_node = frlist_node_create(NULL);

    p_st_node = frlist_node_create(p_st_ct);

    st_stat_all.p_st_hash_l7 = frhash2_create2(5);

    pthread_spin_init(&p_st_ct->lock,0);
    i_ret = __hash_node_all_run(p_st_node,&st_stat_all);
    pthread_spin_destroy(&p_st_ct->lock);

    CU_ASSERT_EQUAL(i_ret,1);
    CU_ASSERT_EQUAL(st_stat_all.ui_all_conn_count,1);
    CU_ASSERT_EQUAL(st_stat_all.ui_dead_conn_count,1);

    p_st_node_l7_proto = frhash2_getLong(st_stat_all.p_st_hash_l7, -2);

    p_st_conn_stat_l7 = (Frct_conn_stat_l7 *)(p_st_node_l7_proto->data);

    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_all_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_dead_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->i_l7_proto,-2);

    frhash2_free2(st_stat_all.p_st_hash_l7,__hash_node_conn_stat_free);
    frfree(p_st_ct);
    frfree(p_st_node);
    return;

}

void case4(void)
{
    Conntrack *p_st_ct = NULL;
    L7_module *p_st_l7_module = NULL;
    L7_analyzer *p_l7analyzer = NULL;

    Frlist_node *p_st_node = NULL;
    Fr_stat_all st_stat_all = {0,0,NULL};
    int i_ret = 0;
    Frct_conn_stat_l7 *p_st_conn_stat_l7 = NULL;
    Frlist_node *p_st_node_l7_proto=NULL;

    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module, 0x00, sizeof(L7_module));

    p_st_l7_module->i_l7_proto = 1433;

    p_l7analyzer = (L7_analyzer *)frmalloc(sizeof(L7_analyzer));
    memset(p_l7analyzer, 0x00, sizeof(L7_analyzer));

    p_l7analyzer->p_st_l7_module = p_st_l7_module;



    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));

    gettimeofday (&p_st_ct->st_connection_lastskb_time, NULL);
    p_st_ct->st_connection_lastskb_time.tv_sec -= 100;

    p_st_node = frlist_node_create(p_l7analyzer);

    p_st_node = frlist_node_create(p_st_ct);

    st_stat_all.p_st_hash_l7 = frhash2_create2(5);

    pthread_spin_init(&p_st_ct->lock,0);
    i_ret = __hash_node_all_run(p_st_node,&st_stat_all);
    pthread_spin_destroy(&p_st_ct->lock);

    CU_ASSERT_EQUAL(i_ret,1);
    CU_ASSERT_EQUAL(st_stat_all.ui_all_conn_count,1);
    CU_ASSERT_EQUAL(st_stat_all.ui_dead_conn_count,1);

    frhash2_free2(st_stat_all.p_st_hash_l7,__hash_node_conn_stat_free);
    frfree(p_st_ct);
    frfree(p_st_node);
    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__hash_node_all_run 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);
   	CU_add_test(pSuite, "case4", case4);


    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}